<?php
namespace jy_chatgpt;
defined('BY_JYA') or exit('error');
class wenda extends \plugin{
	public function get_csvitems($rows){
			$num = count($rows);
			$i = 0;
			$colsIndex = $rows[1];
			$rows = array_slice($rows, 1, count($rows) - 1);
			$items = array();
			$num = 0;
			foreach ($rows as $rownu => $rv) {
				$item = array();
				foreach ($colsIndex as $cols => $cv) {
						$item[$cv]=$rv[$cols];
				}
				$items[] = $item;
				++$num;
			}
			return $items;
	}
	public function daoru($post=array()){
		if(!$post['excel']){
			exi('请先上传CSV文件！','error');
		}
		$localexcel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel($localexcel);
		$res=$this->get_csvitems($excel);
		if(!$res[0]['问题(已加密)'] && !$res[0]['答案(已加密)']){
			$excel=cfc('tools')->importExcel($localexcel,true);
			$res=$this->get_csvitems($excel);
			if(!$res[0]['问题(已加密)'] && !$res[0]['答案(已加密)']){
				cfc('files')->source_del($post['excel']);
				exi('导入文件异常，内容为空！','error');
			}
		}
		cfc('files')->source_del($post['excel']);
		$this->csv_add($res);
		return '';
	}
	public function csv_add($rows){
		foreach($rows as $item){
			if(!$item['问题(已加密)']){
				//exi('有数据不存在姓名！','error');
			}
			$data=array(
				'paixu'=>trim($item['排序']),
				'title'=>base64_decode(trim($item['问题(已加密)'])),
				'msg'=>base64_decode(trim($item['答案(已加密)'])),
				'status'=>1,
			);
			$isadd=pdo_get('jy_chatgpt_wenda',array('uniacid'=>$_SESSION['uniacid'],'title'=>$data['title']),array('id'));
			if($isadd['id']){
				continue;
			}	
			if($isadd['id']){
				//pdo_update('jy_chatgpt_wenda',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}else{
				$data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=TIMESTAMP;
				pdo_insert('jy_chatgpt_wenda',$data);
			}
		}
		return '';
	}
}